package com.legal.service.practice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.legal.domain.po.QuestionCategory;
import com.legal.domain.vo.QuestionCategoryVO;
import com.legal.mapper.practice.QuestionCategoryMapper;
import com.legal.service.practice.IQuestionCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.legal.service.practice.IQuestionService;
import com.legal.service.practice.IUserPracticeStatsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 题库分类表 服务实现类
 * </p>
 *
 * @author xiaopeng
 * @since 2025-06-27
 */
@Service
@RequiredArgsConstructor
public class QuestionCategoryServiceImpl extends ServiceImpl<QuestionCategoryMapper, QuestionCategory> implements IQuestionCategoryService {

    private final IUserPracticeStatsService userPracticeStatsService;

    private final IQuestionService questionService;

    /**
     * 获取热门分类列表
     * @return
     */
    @Override
    public List<QuestionCategoryVO> getHotCategories() {
        // 1. 获取热门分类编号列表
        List<String> categoryNos = userPracticeStatsService.getHotCategoryNo();

        if (CollectionUtils.isEmpty(categoryNos)) {
            return Collections.emptyList();
        }

        // 2. 查询分类基本信息
        List<QuestionCategory> categories = this.list(
                new LambdaQueryWrapper<QuestionCategory>()
                        .in(QuestionCategory::getCategoryNo, categoryNos)
                        .eq(QuestionCategory::getIsActive, 1)
        );

        // 3. 查询每个分类的问题数量
        Map<String, Long> questionCountMap = questionService.countByCategoryNos(categoryNos);

        // 4. 构建VO列表并保持热门排序(categoryNos的顺序)
        // 4.1 创建分类编号-分类映射
        Map<String, QuestionCategory> categoryMap = categories.stream()
                .collect(Collectors.toMap(QuestionCategory::getCategoryNo, Function.identity()));

        // 4.2 创建VO列表 - 并保持热门排序(categoryNos的顺序)
        return categoryNos.stream()
                .map(categoryNo -> {
                    QuestionCategory category = categoryMap.get(categoryNo);
                    return category != null ?
                            new QuestionCategoryVO(category, questionCountMap.getOrDefault(categoryNo, 0L).intValue()) :
                            null;
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    @Override
    public List<QuestionCategoryVO> listWithQuestionCount() {
        // 1. 查询所有启用的分类
        List<QuestionCategory> categories = this.list(
                new LambdaQueryWrapper<QuestionCategory>()
                        .eq(QuestionCategory::getIsActive, 1)
                        .orderByAsc(QuestionCategory::getSortOrder)
        );

        if (CollectionUtils.isEmpty(categories)) {
            return Collections.emptyList();
        }

        // 2. 获取分类编号列表
        List<String> categoryNos = categories.stream()
                .map(QuestionCategory::getCategoryNo)
                .collect(Collectors.toList());

        // 3. 统计每个分类的问题数量
        Map<String, Long> questionCountMap = questionService.countByCategoryNos(categoryNos);

        // 4. 构建VO列表
        return categories.stream()
                .map(category -> new QuestionCategoryVO(
                        category,
                        questionCountMap.getOrDefault(category.getCategoryNo(), 0L).intValue()
                ))
                .collect(Collectors.toList());
    }
}
